package io.nutz.nutzsite.module.javaee.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.nutz.nutzsite.module.javaee.models.Jmenu;
import io.nutz.nutzsite.module.javaee.services.JmenuService;
import io.nutz.nutzsite.common.base.Result;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;
import io.nutz.nutzsite.common.utils.ShiroUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Date;
/**
 * 栏目、菜单、版块 信息操作处理
 * 
 * @author lrw
 * @date 2020-03-07
 */
@IocBean
@At("/javaee/jmenu")
public class JmenuController {
	private static final Log log = Logs.get();

	@Inject
	private JmenuService jmenuService;
	
	@RequiresPermissions("javaee:jmenu:view")
	@At("/?")
	@Ok("th:/javaee/jmenu/jmenu.html")
	public void index(String id,HttpServletRequest req) {
		Jmenu jmenu=jmenuService.fetch(id);
		req.setAttribute("jee", jmenu);
	}

	/**
	 * 查询栏目、菜单、版块列表
	 */
	@RequiresPermissions("javaee:jmenu:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("name") String name,
					   @Param("beginTime") Date beginTime,
					   @Param("endTime") Date endTime,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(name)){
			//cnd.and("name", "like", "%" + name +"%");
		}
		if(Lang.isNotEmpty(beginTime)){
			cnd.and("create_time",">=", beginTime);
		}
		if(Lang.isNotEmpty(endTime)){
			cnd.and("create_time","<=", endTime);
		}
		return jmenuService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}

	/**
	 * 新增栏目、菜单、版块
	 */
	@At("/add")
	@Ok("th:/javaee/jmenu/add.html")
	public void add( HttpServletRequest req) {

	}

	/**
	 * 新增保存栏目、菜单、版块
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("javaee:jmenu:add")
	@Slog(tag="栏目、菜单、版块", after="新增保存栏目、菜单、版块 id=${args[0].id}")
	public Object addDo(@Param("..") Jmenu jmenu,HttpServletRequest req) {
		try {
			jmenuService.insert(jmenu);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 修改栏目、菜单、版块
	 */
	@At("/edit/?")
	@Ok("th://javaee/jmenu/edit.html")
	public void edit(String id, HttpServletRequest req) {
		Jmenu jmenu = jmenuService.fetch(id);
		req.setAttribute("jmenu",jmenu);
	}

	/**
	 * 修改保存栏目、菜单、版块
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("javaee:jmenu:edit")
	@Slog(tag="栏目、菜单、版块", after="修改保存栏目、菜单、版块")
	public Object editDo(@Param("..") Jmenu jmenu,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(jmenu)){

				jmenuService.update(jmenu);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除栏目、菜单、版块
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("javaee:jmenu:remove")
	@Slog(tag ="栏目、菜单、版块", after= "删除栏目、菜单、版块:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			jmenuService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
